﻿using AutoMapper;
using B.S.DataBase.Myproject.Helper;
using B.S.DataBase.MyProject.Domain.wdx;
using B.S.DataBase.MyProject.Domain.wdx.ViewModels;

using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure;
using MediatR;
using MedicalPCTerminal.Read.API.Appcation.Commands.ResidentCommand;
using SqlSugar;

namespace MedicalPCTerminal.Read.API.Appcation.CommandHandlers.ResidentCommandHandler
{
    public class GetResidentFilesDetailCommandHandler : IRequestHandler<GetResidentFilesDetailCommand, APIResult<View_ResidentFileDetail>>
    {
        /// <summary>
        /// SqlSugar
        /// </summary>
        private readonly SqlSugarContext sqlSugar;

        public GetResidentFilesDetailCommandHandler(SqlSugarContext sqlSugar)
        {
            this.sqlSugar = sqlSugar;
        }

        public Task<APIResult<View_ResidentFileDetail>> Handle(GetResidentFilesDetailCommand request, CancellationToken cancellationToken)
        {
            APIResult<View_ResidentFileDetail> result = new APIResult<View_ResidentFileDetail>();
            var list = sqlSugar.db.Queryable<ResidentFiles>().Where(x => x.ResidentFiles_Id == request.ResidentFiles_Id)
               .LeftJoin<NCDSign>((file, sign) => file.ResidentFiles_Id == sign.Patient_Id)
               .Select((file, sign) => new View_ResidentFileDetail
               {
                   ResidentFiles_Id = file.ResidentFiles_Id,
                   ResidentFiles_Name = file.ResidentFiles_Name,
                   Sex = file.Sex == true ? "男" : "女",
                   IdCard = file.IdCard,
                   Age = file.Age,
                   Address = file.Address,
                   Phone = file.Phone,
                   Birthday = file.Birthday.ToString(),
                   CreateTime = file.CreateTime,
                   Sign_Status = sign.Sign_Status,
                   Doctor = sign.Doctor,
                   Team = sign.Team,
                   Marriage = file.Marriage,
                   BloodType = file.BloodType,
                   InsuredSituation = file.InsuredSituation,
                   job = file.job,
                   Education = file.Education,
                   IfSmoke = file.IfSmoke,
                   IfQuitSmoke = file.IfQuitSmoke,
                   IfPassiveSmoke = file.IfPassiveSmoke,
                   IfDrinkWine = file.IfDrinkWine,
                   IfSport = file.IfSport,
                   Hypertension = file.Hypertension,
                   Diabetes = file.Diabetes,
                   Height = file.Height,
                   Weight = file.Weight,
                   BMI = file.BMI,
                   Waistline = file.Waistline,
                   SystolicPressure = file.SystolicPressure,
                   SiastolicPressure = file.SiastolicPressure,
                   FastingBloodGlucose = file.FastingBloodGlucose,
                   CreateUser = file.CreateUser,
                   UpdateUser = file.UpdateUser,
                   UpdateTime = file.UpdateTime,
                   Sign_StartTime = sign.Sign_StartTime,
                   Sign_EndTime = sign.Sign_EndTime,
                   ManageLevel = sign.ManageLevel,
                   // 使用 SqlFunc.Subquery 进行子查询
                   medicalRecords = SqlFunc.Subqueryable<MedicalRecords>()
                                      .Where(x => x.Patient_Id == file.ResidentFiles_Id)
                                      .ToList()
               });

            result.data = list.ToList().FirstOrDefault();
            // 在查询结果之后进行日期格式化
            if (result.data != null)
            {
                result.data.Birthday = result.data.Birthday.StringToDateTime().DateTimeToString("yyyy-MM-dd HH:mm:ss", "--");
            }
            result.Code = APIEnum.查询成功;
            result.Msg = APIEnum.查询成功.ToString();
            return Task.FromResult(result);
        }
    }
}